#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# OPENCOM.PY                   -- by Dr. ZhuoQing 2020-10-22
#
# Note:
#   opencom COMn baud portname
#
#============================================================

from head import *

comstr = 'COM2'
comname = 'sport'
baudrate = 115200

#------------------------------------------------------------
if len(sys.argv) > 1:
    comstr = sys.argv[1].upper()

if comstr.isdigit():
    comstr = 'COM%s'%comstr

if len(sys.argv) > 2:
    baudrate = int(sys.argv[2])

if len(sys.argv) > 3:
    comname = sys.argv[3]

#------------------------------------------------------------
tspinsii("import serial")
tspinsii("from _ast import Or")
tspinsii("from serial.serialutil import SerialException\r\n")



tspinsii("\r\n#------------------------------------------------------------\r\n")
tspinsii("%s = serial.Serial()"%comname)
tspinsii("%s.baudrate = %d"%(comname, baudrate))
tspinsii("%s.timeout = 0.05\r\n"%comname)
tspinsii("try:")
tspinsii("    %s.port = '%s'"%(comname, comstr))
tspinsii("except:")
tspinsii("    printf('Set %s port %s error. ')\r\n\r\n"%(comname, comstr))
tspinsii("try:")
tspinsii("    %s.open()"%comname)
tspinsii("except serial.serialutil.SerialException:")
tspinsii("    printf('Open %s port %s error.')"%(comname, comstr))
tspinsii("else:")
tspinsii("    printf('Open %s port %s Ok.')\r\n"%(comname, comstr))
tspinsii("#------------------------------------------------------------\r\n\r\n")





#------------------------------------------------------------
#        END OF FILE : OPENCOM.PY
#============================================================
